#include <iostream>
#include <vector>
#include <algorithm>

#include "character.h"


int split_character_latin(
  const std::string& input, 
  std::vector<std::string>& output) 
{
  std::for_each(input.begin(), input.end(), [&output](char c){
    output.push_back(std::string(1, c));
  });

  return output.size();
}



int split_character_utf8(
  const std::string& input, 
  std::vector<std::string>& output) 
{
  std::string ch; 
  for (size_t i = 0, len = 0; i < input.length(); i += len) {
    unsigned char byte = (unsigned)input[i];
    if (byte >= 0xFC) // lenght 6
      len = 6;  
    else if (byte >= 0xF8)
      len = 5;
    else if (byte >= 0xF0)
      len = 4;
    else if (byte >= 0xE0)
      len = 3;
    else if (byte >= 0xC0)
      len = 2;
    else
      len = 1;
    ch = input.substr(i, len);
    output.push_back(ch);
  }   
  return output.size();
}


int split_character(
  const std::string& input, 
  std::vector<std::string>& output,
  CHARSET_E chs) 
{
  switch (chs) {
    case CHARSET_UTF8:
      return split_character_utf8(input, output);
    case CHARSET_LATIN1:
      return split_character_latin(input, output);
    default:
      return -1;
  }
}

